package com.cq.school.repository;

import com.cq.school.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: huke
 * Date: 13-11-28
 * Time: 下午11:01
 * To change this template use File | Settings | File Templates.
 */
public interface ArticleDao extends PagingAndSortingRepository<Article, Long>, JpaSpecificationExecutor<Article> {
    Page<Article> findByCategory(int category, Pageable pageRequest);
    @Query(value = "SELECT * FROM article WHERE school_id=?1 order by id desc limit ?2", nativeQuery = true)
    List<Article>  listSchoolArticle(long schoolId,int size);
    @Query(value = "(SELECT * FROM article WHERE id>?1 order by id asc limit 1) UNION (SELECT * FROM article WHERE id<?1 order by id desc limit 1)", nativeQuery = true)
    List<Article> getNexPre(long id);

    @Modifying
    @Query("update Article a set a.hits = a.hits+1 where a.id = ?1")
    void updateHits(long id);
}
